package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.UserIntegral;
import cn.wolfcode.trip.base.domain.UserOrder;
import cn.wolfcode.trip.base.mapper.UserIntegralMapper;
import cn.wolfcode.trip.base.mapper.UserOrderMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserOrderServiceImpl implements IUserOrderService {

    @Autowired
    private UserOrderMapper userOrderMapper;
    @Autowired
    private UserIntegralMapper integralMapper;

    @Override
    public void saveOrUpdate(UserOrder empty) {

        if(empty.getId() == null){
            //新增,此时还没有订单号
            //uuid随机数给订单号
            int machineId = 1;//最大支持1-9个集群机器部署
            int hashCodeV = UUID.randomUUID().toString().hashCode();
            if(hashCodeV < 0) {//有可能是负数
                hashCodeV = - hashCodeV;
            }
            // 0 代表前面补充0
            // 4 代表长度为4
            // d 代表参数为正数型
            String str = machineId + String.format("%015d", hashCodeV);
            empty.setOrderNumber(str);
            userOrderMapper.insert(empty);
        }else {
            userOrderMapper.updateByPrimaryKey(empty);
        }
    }



    @Override
    public PageInfo<UserOrder> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<UserOrder> list = userOrderMapper.selectForList(qo);
        return new PageInfo<UserOrder>(list);
    }

    @Override
    public UserOrder selectByProductId(Long id) {
        return userOrderMapper.selectByProductId(id);
    }

    @Override
    public UserOrder selectByIndentId(Long id) {
        return userOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateByIndentId(Long id,Integer state,Long userId) {
        //先扣钱
        //查出该订单的商品积分
        UserOrder userOrder = userOrderMapper.selectByPrimaryKey(id);
        //订单商品积分
        Integer integral = userOrder.getIntegral();
        //查询出用户的积分
        UserIntegral userIntegralObject= integralMapper.selectByPrimaryKey(userId);
        Integer userIntegral = userIntegralObject.getIntegral();
        //相减得到新的积分数
        int newIntegral = userIntegral-integral;

        //修改用户的积分
        integralMapper.updateIntegral(newIntegral,userId);

        //修改订单状态
        userOrderMapper.updateByIndentId(id,state);
    }

    @Override
    public void saveOrUpdateLeading(UserOrder empty) {
        if(empty.getId() == null){
            userOrderMapper.insert(empty);
        }else {
            userOrderMapper.updateByPrimaryKey(empty);
        }
    }

    @Override
    public UserOrder selectByOrderNumber(String orderNumber) {
        return userOrderMapper.selectByOrderNumber(orderNumber);
    }

    @Override
    public UserOrder selectByUUID(String orderNumber) {
        return userOrderMapper.selectByUUID(orderNumber);
    }
}
